package userHelp

import (
	myConfig "common/config"
	myHelper "common/helper"
	myredis "common/redis"
	"strconv"
	"time"
)

/*
用户VIP 属性
*/

var g_tbl_member_vip = "tbl_member_vip:"

type TblMemberVIP struct {
	Uid            string `json:"uid" db:"uid" cbor:"uid"`
	Level          int64  `json:"level" db:"level" cbor:"level"`
	WeekBonusTime  int64  `json:"weekBonusTime" db:"weekBonusTime" cbor:"weekBonusTime"`
	MonthBonusTime int64  `json:"monthBonusTime" db:"monthBonusTime" cbor:"monthBonusTime"`
	UpBonusLevel   int64  `json:"upBonusLevel" db:"upBonusLevel" cbor:"upBonusLevel"`
}

func GetMonthBonusTime(uid string) int64 {
	LoadUserToRedis(uid)
	return myredis.GetUserFieldInt64(uid, g_tbl_member_vip+"monthBonusTime")
}

func SetMonthBonusTime(uid string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_vip+"monthBonusTime", strconv.FormatInt(time.Now().Unix(), 10))
}

func GetWeekBonusTime(uid string) int64 {
	LoadUserToRedis(uid)
	return myredis.GetUserFieldInt64(uid, g_tbl_member_vip+"weekBonusTime")
}

func SetWeekBonusTime(uid string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_vip+"weekBonusTime", strconv.FormatInt(time.Now().Unix(), 10))
}

func GetUpBonusLevel(uid string) int64 {
	LoadUserToRedis(uid)
	return myredis.GetUserFieldInt64(uid, g_tbl_member_vip+"upBonusLevel")
}

func SetUpBonusLevel(uid string, upBonusLevel int64) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_vip+"upBonusLevel", strconv.FormatInt(upBonusLevel, 10))
}

func GetVipLevel(uid string) int64 {
	LoadUserToRedis(uid)
	return myredis.GetUserFieldInt64(uid, g_tbl_member_vip+"level")
}

func AddVipLevel(uid string) {
	LoadUserToRedis(uid)
	myredis.AddUserFieldValueByInt64(uid, g_tbl_member_vip+"level", 1)
}

/*
是否可以领取月奖励
*/
func CanMonthBonus(uid string) bool {
	MonthBonusTime := GetMonthBonusTime(uid)
	vipLevel := GetVipLevel(uid)
	if myConfig.GetVipMonthBonus(vipLevel) == 0 {
		return false
	}
	if myHelper.InSameMonth(MonthBonusTime, time.Now().Unix()) {
		return false
	}

	return true
}

/*
是否可以领取周奖励
*/
func CanWeekBonus(uid string) bool {
	WeekBonusTime := GetWeekBonusTime(uid)
	vipLevel := GetVipLevel(uid)
	if myConfig.GetVipWeekBonus(vipLevel) == 0 {
		return false
	}
	if myHelper.InSameWeek(WeekBonusTime, time.Now().Unix()) {
		return false
	}

	return true
}

/*
是否可以领取升级奖励
*/
func CanUpBonus(uid string) bool {
	LoadUserToRedis(uid)
	vipLevel := GetVipLevel(uid)
	UpBonusLevel := GetUpBonusLevel(uid)
	if vipLevel > UpBonusLevel && myConfig.GetVipUpBonus(vipLevel) > 0 {
		return true
	} else {
		return false
	}
}

/*
VIP 升级
*/
func ProcessVipUpLevel(uid string) {
	LoadUserToRedis(uid)

	vipLevel := GetVipLevel(uid)
	if vipLevel >= myConfig.GetMaxVipLevel() {
		return
	}

	NeedRunning := myConfig.GetVipUpLevelNeedRunning(vipLevel + 1)
	TotalWaged := GetTotalWaged(uid)

	if TotalWaged >= NeedRunning {
		AddVipLevel(uid)
	}
}
